package com.zzl.finance.data.service.common.impl;

import com.zzl.finance.data.constant.OperateButtonEnum;
import com.zzl.finance.data.constant.status.InstitutyStatusEnum;
import com.zzl.finance.data.constant.type.MenuButtonEnum;
import com.zzl.finance.data.dto.FinanceInstitutyPageDto;
import com.zzl.finance.data.service.common.BaseHandStatusService;
import com.zzl.finance.data.util.BaseUtils;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.KeyValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author : gull
 * @date : 2022/2/18 13:30
 */
@Service
public class BaseHandStatusServiceImpl implements BaseHandStatusService {

    @Value("#{'${config.buttonAuth}'.equals('off')}")
    private boolean buttonAuth;

    /**
     * 设置 资方管理按钮操作
     *
     * @param financeInstitutyPageDtoList 资方管理按钮
     */
    @Override
    public void setOpenTicketCheckOperates(List<FinanceInstitutyPageDto> financeInstitutyPageDtoList) throws SysException {
        for (Object financeInstituty : financeInstitutyPageDtoList) {
            List<KeyValue> keyValues = new ArrayList<>();
            KeyValue keyValue = new KeyValue(OperateButtonEnum.DETAILS.getCode(), null, OperateButtonEnum.DETAILS.getValue());
            keyValues.add(keyValue);
            KeyValue keyValue1 = new KeyValue(OperateButtonEnum.EDIT.getCode(), null, OperateButtonEnum.EDIT.getValue());
            keyValues.add(keyValue1);
            Integer code = BaseUtils.getInstitutyStatus(financeInstituty).getCode();
            //启用状态的金融机构，有冻结按钮
            if (InstitutyStatusEnum.INSTITUTY_STATUS_ENABLE.getCode().equals(code)) {
                keyValues.add(new KeyValue(OperateButtonEnum.FROZEN.getCode(), null, OperateButtonEnum.FROZEN.getValue()));
            }
            BaseUtils.setOperateButton(financeInstituty, keyValues);
        }
    }

    /**
     * 设置 资方管理按钮操作
     *
     * @param financeInstitutyPageDtoList 资方管理按钮
     */
    @Override
    public void setFinanceInstitutyOperates(List<FinanceInstitutyPageDto> financeInstitutyPageDtoList, HttpSession session) {

        if (buttonAuth && !CollectionUtils.isEmpty(financeInstitutyPageDtoList)) {
            //编辑
            boolean buttonAuthFlag = BaseUtils.getButtonAuth(session, MenuButtonEnum.FINANCE_INSTITUTY_EDIT_BUTTON.getCode());
            //冻结
            boolean buttonAuthFlag2 = BaseUtils.getButtonAuth(session, MenuButtonEnum.FINANCE_INSTITUTY_FROZEN_BUTTON.getCode());
            //启用
            boolean buttonAuthFlag3 = BaseUtils.getButtonAuth(session, MenuButtonEnum.FINANCE_INSTITUTY_ENABLE_BUTTON.getCode());
            for (FinanceInstitutyPageDto checkPage : financeInstitutyPageDtoList) {
                List<KeyValue> keyValues = new ArrayList<>();
                KeyValue keyValue1 = new KeyValue(OperateButtonEnum.DETAILS.getCode(), null, OperateButtonEnum.DETAILS.getValue());
                keyValues.add(keyValue1);
                if (buttonAuthFlag) {
                    keyValues.add(new KeyValue(OperateButtonEnum.EDIT.getCode(), null, OperateButtonEnum.EDIT.getValue()));
                }
                //状态为已启用，并且有按钮权限
                if (InstitutyStatusEnum.INSTITUTY_STATUS_ENABLE.getCode().equals(checkPage.getInstitutyStatus()) && buttonAuthFlag2) {
                    keyValues.add(new KeyValue(OperateButtonEnum.FROZEN.getCode(), null, OperateButtonEnum.FROZEN.getValue()));
                }
                if (InstitutyStatusEnum.INSTITUTY_STATUS_STOP.getCode().equals(checkPage.getInstitutyStatus()) && buttonAuthFlag3) {
                    keyValues.add(new KeyValue(OperateButtonEnum.ENABLE.getCode(), null, OperateButtonEnum.ENABLE.getValue()));
                }
                checkPage.setOperateButton(keyValues);

            }
        }

    }
}
